module Ethereum

  class Initializer
    attr_accessor :contracts, :file, :client

    def initialize(file, client = Ethereum::Singleton.instance)
      @client = client
      sol_output = Solidity.new.compile(file)
      contracts = sol_output.keys

      @contracts = []
      main_contract_name = File.basename(file).gsub('.sol', '')
      contract_main = []
      contract_oth = []
      contracts.each do |contract|
        abi = JSON.parse(sol_output[contract]["abi"] )
        name = contract
        code = sol_output[contract]["bin"]
        #取得当前合约为主合约
        if name == main_contract_name
          contract_main << Contract.new(name, code, abi, @client)
        else
          contract_oth << Contract.new(name, code, abi, @client)
        end

      end
      @contracts = contract_main + contract_oth
      @contracts
    end

    def build_all
      @contracts.each do |contract|
        contract.build
      end
    end

  end
end
